package com.store.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.store.app.R;
import com.store.app.bean.StorePreviewFeatureGoodBean;

import java.util.List;
import java.util.Map;

/**
 * 便利店中的特色商品
 */
public class FeatureAdapter extends BaseAdapter {


    private List<StorePreviewFeatureGoodBean.ListBean> goodList;
    private Context context;
    private boolean isbianlidian=false;
    private Map<String,String> urlMap;
    public FeatureAdapter(List<StorePreviewFeatureGoodBean.ListBean> goodList, Context context,
                         Map<String,String> map) {
        this.goodList = goodList;
        this.context = context;
        this.urlMap = map;
    }
    @Override
    public int getCount() {
        return goodList == null ? 0 : goodList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.feature_production_item, null);
            viewHolder.ivGood = (ImageView) convertView.findViewById(R.id.iv_feature);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
            viewHolder.addBtnCard = (ImageView) convertView.findViewById(R.id.addBtnCard);
            viewHolder.tv_addBtnCard= (TextView) convertView.findViewById(R.id.addBtnCard_tv);
            viewHolder.tv_feature_return=(TextView) convertView.findViewById(R.id.iv_feature_return);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.addBtnCard.setVisibility(View.VISIBLE);
        viewHolder.tv_addBtnCard.setVisibility(View.VISIBLE);

        final StorePreviewFeatureGoodBean.ListBean featureGood = goodList.get(position);
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.default_picture_s)
                .showImageForEmptyUri(R.drawable.default_picture_s)
                .showImageOnFail(R.drawable.default_picture_s) .build();
        ImageLoader.getInstance().displayImage((urlMap.get(featureGood.getImage_info())),viewHolder.ivGood,options);


        viewHolder.tvTitle.setText(featureGood.getItem_name());
        viewHolder.tvPrice.setText("¥" + featureGood.getMarket_price());
        if(!TextUtils.isEmpty(featureGood.getSpecification())){
            viewHolder.tv_detail.setText("规格:" +featureGood.getSpecification());

        }else {
            viewHolder.tv_detail.setText("规格:");
        }
        String s_rebate=featureGood.getRebate();
        if(TextUtils.isEmpty(s_rebate)||s_rebate.equals("0")||s_rebate.equals("0.0")||s_rebate.equals("0.00")) {
            viewHolder.tv_feature_return.setVisibility(View.GONE);
        }else{
            viewHolder.tv_feature_return.setVisibility(View.VISIBLE);
            viewHolder.tv_feature_return.setText("返¥"+s_rebate+"元");
        }

        return convertView;
    }

    class ViewHolder {
        private ImageView ivGood;
        private TextView tvTitle, tvPrice, tv_detail,tv_addBtnCard,tv_feature_return;
        private ImageView addBtnCard;
    }
}
